<?php

namespace app\api\controller;

use app\api\model\wanlshop\Order;
use app\api\model\wanlshop\Pay;
use app\api\model\wanlshop\Paylog;
use app\api\model\wanlshop\Shop;
use app\common\controller\Api;
use addons\epay\library\Service;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        // 切换v3支付
        set_addon_config('epay', ['version'=>'v3'], false);
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    //过售后的订单进行分账
    public function taskFenzhang()
    {
        $config = get_addon_config('wanlshop');

        //商城订单分账
        $order_list = Order::where('taketime','<',time()-$config['order']['customer'] * 86400)
            ->where('fenzhang_status',0)
            ->where('state','in','4,6')->select();
        //dump($order_list);die;
        foreach ($order_list as $k=>$v){
            $pay = Pay::get(['order_no'=>$v['order_no']]);
            if ($pay){

                $refund = model('app\api\model\wanlshop\Refund')
                    ->where(['order_id' => $v['id'], 'state' => 4, 'order_type' => 'goods'])
                    ->sum('price');

                if ($pay->pay_type == 1){

                    //分账
                    $shop = Shop::get($pay['shop_id']);
                    $config = Service::getConfig('wechat');
                    $fenzhang_price = bcsub($pay['price'],$refund,2);
                    $receivers = [
                        [
                            'type'=>'MERCHANT_ID',
                            'account'=>$shop['mch_id'],
                            'amount'=>$fenzhang_price*100,
                            'description'=>'商城订单分账'
                        ]
                    ];
                    $fenzhang_data = [
                        'out_order_no'  => $pay['pay_no'],
                        'transaction_id'=> $pay['trade_no'],
                        'receivers'     => $receivers,
                        'unfreeze_unsplit'=>false
                    ];

                    $response = \Yansongda\Pay\Pay::wechat($config)->profitsharing($fenzhang_data);

                    if ($response->state=='FINISHED'||$response->state=='PROCESSING'){
                        if ($response->state=='FINISHED'){
                            $fenzhang_status = 1;
                        } else {
                            $fenzhang_status = 2;
                        }
                        $pay->fenzhang_status = $fenzhang_status;
                        $pay->fenzhang_order_id = $response->order_id;
                        Order::where(['id'=>$v['id']])->update(['fenzhang_status'=>$fenzhang_status,'fenzhang_order_id'=>$response->order_id]);
                    }
                }
            }

        }

        //线上/线下买单分账状态
        $paylog_list = Paylog::where(['pay_status'=>1,'paytype'=>'wechat'])
            ->where('fenzhang_status','in','0,2')
            ->select();
        foreach ($paylog_list as $k=>$v){
            $shop = Shop::get($v['shop_id']);
            $config = Service::getConfig('wechat');
            $receivers = [
                [
                    'type'=>'MERCHANT_ID',
                    'account'=>$shop['mch_id'],
                    'amount'=>$v['payamount']*100,
                    'description'=>'线上/线下买单分账'
                ]
            ];
            $response = \Yansongda\Pay\Pay::wechat($config)->profitsharing([
                'out_order_no'  => $v['order_no'],
                'transaction_id'=> $v['transaction_id'],
                'receivers'     => $receivers,
                'unfreeze_unsplit'=>false
            ]);
            if ($response->state=='FINISHED'||$response->state=='PROCESSING'){
                if ($response->state=='FINISHED'){
                    $fenzhang_status = 1;
                } else {
                    $fenzhang_status = 2;
                }
                $order = Paylog::get($v['id']);
                $order->fenzhang_status = $fenzhang_status;
                $order->fenzhang_order_id = $response->order_id;
                $order->save();
            }
        }
        $this->success();
    }

}
